<?php


use App\Library\Utils\Arr;
use Phinx\Seed\AbstractSeed;
use Faker\Factory as FakerFactory;

class DanmuSeeder extends AbstractSeed
{
    public function getDependencies(): array
    {
        return [
            'CourseSeeder',
            'ChapterSeeder',
            'UserSeeder',
        ];
    }

    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * https://book.cakephp.org/phinx/0/en/seeding.html
     */
    public function run()
    {
        $faker = FakerFactory::create('zh_CN');

        $courses = $this->fetchAll('SELECT * FROM kg_course');
        $chapters = $this->fetchAll('SELECT * FROM kg_chapter');
        $users = $this->fetchAll('SELECT * FROM kg_user');

        $rows = [];
        for ($i = 0; $i < 100; $i++) {
            $rows[] = [
                'course_id' => Arr::rand($courses)['id'],
                'chapter_id' => Arr::rand($chapters)['id'],
                'owner_id' => Arr::rand($users)['id'],
                'time' => rand(0, 100),
                'text' => $faker->text(60),
                'color' => 'white',
                'size' => 0,
                'position' => 0,
                'published' => 1,
                'create_time' => $faker->unixTime,
                'update_time' => $faker->unixTime,
            ];
        }
        $this->table('kg_danmu')->insert($rows)->saveData();
    }

}
